//
//  HNSH_ChangePayPwdViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/23.
//

#import "HNSH_ChangePayPwdViewController.h"
#import "HNSH_PayPasswordView.h"

@interface HNSH_ChangePayPwdViewController ()<PayPasswordViewDelegate>
/**  */
@property (nonatomic, strong) UILabel * firLabel;
/**  */
@property (nonatomic, strong) HNSH_PayPasswordView * firView;
/**  */
@property (nonatomic, strong) UILabel * secLabel;
/**  */
@property (nonatomic, strong) HNSH_PayPasswordView * secView;
/** */
@property (nonatomic, strong) UIButton * confirmBtn;

@end

@implementation HNSH_ChangePayPwdViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    if ([ST_UserManager shareInstance].user.pay_password_status) {
        [self showNavTitle:@"修改支付密码" showLeftItem:YES];
    } else {
        [self showNavTitle:@"设置支付密码" showLeftItem:YES];
    }
    
    [self addAllViews];

}

//MARK: Action Methods
- (void)confirmAction{
    [self.view endEditing:YES];
    
    if (![self.firView.saveStore.copy isEqualToString:self.secView.saveStore.copy]) {
        [self.view makeToast:@"两次输入的密码不一致"];
        return;
    }
    [HNSH_UserNetServer requestUserChangePaymentPwdWithSms_code:self.smsCode pwd:self.secView.saveStore.copy success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [[ST_UserManager shareInstance] loadUserInfoComplate:^{
                
            }];
            [self.navigationController popToViewController:self.navigationController.viewControllers[self.navigationController.viewControllers.count-2] animated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.view addSubview:self.firLabel];
    [self.view addSubview:self.firView];
    [self.view addSubview:self.secLabel];
    [self.view addSubview:self.secView];
    [self.view addSubview:self.confirmBtn];
}

//MARK: Getter Methods
- (UILabel *)firLabel{
    if (!_firLabel) {
        _firLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, kNavBarAndStatusBarHeight + 16, kScreenWidth - 32, 20)];
        _firLabel.font = [UIFont systemFontOfSize:14];
        _firLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _firLabel.numberOfLines = 0;
        NSString * phone = [NSString transPhoneWithMobile:[ST_UserManager shareInstance].user.phone];
        _firLabel.text = [NSString stringWithFormat:@"请为账户%@设置6位数字的支付密码:", phone];
    }
    return _firLabel;
}

- (HNSH_PayPasswordView *)firView{
    if (!_firView) {
        CGFloat w = 42 * 6;
        _firView = [[HNSH_PayPasswordView alloc] initWithFrame:CGRectMake((kScreenWidth - w) / 2, CGRectGetMaxY(self.firLabel.frame) + 16, w, 42)];
        _firView.backgroundColor = [UIColor clearColor];
        _firView.passwordNumber = 6;
        _firView.squareSize = 42;
        _firView.delegate = self;
    }
    return _firView;
}

- (UILabel *)secLabel{
    if (!_secLabel) {
        _secLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(self.firView.frame) + 24, kScreenWidth - 32, 20)];
        _secLabel.font = [UIFont systemFontOfSize:14];
        _secLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        _secLabel.numberOfLines = 0;
        _secLabel.text = @"再次输入支付密码:";
    }
    return _secLabel;
}

- (HNSH_PayPasswordView *)secView{
    if (!_secView) {
        CGFloat w = 42 * 6;
        _secView = [[HNSH_PayPasswordView alloc] initWithFrame:CGRectMake((kScreenWidth - w) / 2, CGRectGetMaxY(self.secLabel.frame) + 16, w, 42)];
        _secView.backgroundColor = [UIColor clearColor];
        _secView.passwordNumber = 6;
        _secView.squareSize = 42;
        _secView.delegate = self;
    }
    return _secView;
}

- (UIButton *)confirmBtn{
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmBtn.frame = CGRectMake(30, kScreenHeight - 50 - 23 - kBottomSafeHeight, kScreenWidth - 60, 50);
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _confirmBtn.backgroundColor = [UIColor appGreenColor];
        _confirmBtn.layer.cornerRadius = 25;
        _confirmBtn.layer.masksToBounds = YES;
        [_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

//MARK: PayPasswordViewDelegate Methods
/** 监听输入完成时 */
- (void)passwordCompleteInput:(HNSH_PayPasswordView *)password{
    if (password == self.firView) {
        [self.firView cancelFirstResponder];
        [self.secView becomeFirstResponder];
    } else {
        [self.secView cancelFirstResponder];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
